<?php if($user->access!=0){ ?>
<div id="page_file">
<?php
	//cas ou l'utilisateur choisi le trie qu'il veut
	if ($_GET['server']=="choice"){
		?>
		<table>
		<tr>
			<td class="title" >Trier les fichiers par</td>
		</tr>
		<tr>
			<td><a href="index.php?page=<?php print $_GET['page']; ?>&server=allserver">All Server</a></td>
		</tr>
		<?php
		$sql_query=mysql_query("Select name from ".DATABASE_TABLE_SERVER." ORDER BY `name` ASC") or die(mysql_error());
		while ($query = mysql_fetch_array($sql_query) ){
			echo '<tr>';
						echo "<td><a href=\"index.php?page=".$_GET['page']."&server=".$query['name']."\">".$query['name']."</a></td>";
			echo '</tr>';
		}
		?> </table><?php
	}
	elseif ($_GET['server']!=""){
		if ($_GET['server']=="allserver"){
			$server="%";
		}
		else{
			$server=mysql_real_escape_string(htmlspecialchars($_GET['server']));
		}
		
		
		$sql_query=mysql_query("Select name,last_update from ".DATABASE_TABLE_SERVER." WHERE name = '".$server."' ORDER BY `name` ASC") or die(mysql_error());
		while ($query = mysql_fetch_array($sql_query) ){
				$base_server_name = $query['name'];
				$base_server_last_update = $query['last_update'];
		}
		
		
		//check trie des servers
		if (isset($_GET['sortby']) and $_GET['sortby'] ){
			$sortby = mysql_escape_string($_GET['sortby']);	
		}
		else{
			$sortby = mysql_escape_string('name');
		}
		
		
		if (isset($_GET['orderby']) and $_GET['orderby'] != "" ){
			$orderby = mysql_escape_string($_GET['orderby']);	
		}
		else{
			$orderby = mysql_escape_string('ASC');
		}
		
		
		if ($_GET['page'] == "films"){
			$def_array	= $def_film;
			$sql_query=mysql_query("SELECT ".DATABASE_TABLE_FILE.".name, ".DATABASE_TABLE_FILE.".taille, ".DATABASE_TABLE_FILE.".date, ".DATABASE_TABLE_FILE.".server, ".DATABASE_TABLE_SERVER.".address 
			FROM ".DATABASE_TABLE_FILE." JOIN ".DATABASE_TABLE_SERVER." ON ".DATABASE_TABLE_FILE.".server = ".DATABASE_TABLE_SERVER.".name 
			WHERE ".DATABASE_TABLE_FILE.".server like '".$server."' AND (".array_in_where_clause(DATABASE_TABLE_FILE.".name",$def_array).") ORDER BY ".$sortby." ".$orderby.",name ") or die(mysql_error());
			
		}
		elseif($_GET['page'] == "other"){
			$def_array	= $def_film;
			$sql_query=mysql_query("SELECT ".DATABASE_TABLE_FILE.".name, ".DATABASE_TABLE_FILE.".taille, ".DATABASE_TABLE_FILE.".date, ".DATABASE_TABLE_FILE.".server, ".DATABASE_TABLE_SERVER.".address 
			FROM ".DATABASE_TABLE_FILE." JOIN ".DATABASE_TABLE_SERVER." ON ".DATABASE_TABLE_FILE.".server = ".DATABASE_TABLE_SERVER.".name 
			WHERE ".DATABASE_TABLE_FILE.".server like '".$server."' AND (".array_in_where_not_clause(DATABASE_TABLE_FILE.".name",$def_array).") ORDER BY ".$sortby." ".$orderby.",name ") or die(mysql_error());
			
		}
		elseif ($_GET['page'] == "search"){
			if ($_GET['file'] == ""){
				$def_array[0] == '%';
			}
			else{
				$def_array=explode(" ",mysql_escape_string($_GET['file']));
			}
			if (isset($_GET['orderby']) and $_GET['orderby'] != "" ){
				$orderby = mysql_escape_string($_GET['orderby']);	
			}
			else{
				$orderby = mysql_escape_string('DESC');
			}
			
			if (isset($_GET['sortby']) and $_GET['sortby'] != "" ){
				$sortby = mysql_escape_string($_GET['sortby']);	
			}
			else{
				$sortby = mysql_escape_string('pertinence');
			}
			//requete by Polaris and google
			$sql_query=mysql_query("SELECT name,taille,server,date,((".array_in_where_plus_clause('name',$def_array).")*100/".count($def_array).") AS pertinence ,CONCAT(round((".array_in_where_plus_clause('name',$def_array).")*100/".count($def_array)."),'%') AS found_count
										FROM searchserv_file
										WHERE ( ".array_in_where_OR_clause('name',$def_array)." )
										ORDER BY ".$sortby." ".$orderby) or die(mysql_error());
			
		
		}
		
		?> 
		<table>
		<caption>List of files of <?php print $_GET['server']; ?><?php if ($server != "%" ){?> Last update : <?php print date("d/m/y  G\hi\m",$base_server_last_update); } ?> (<?php print mysql_num_rows($sql_query);?> files) <input type="button" value="Refresh" onclick="refresh_serv('<?php print $server; ?>');location.reload();" /> </caption>
		<tr>
			<td><a class="title" href="index.php?page=<?php print $_GET['page']; ?>&file=<?php print $_GET['file']; ?>&server=<?php print $_GET['server']; ?>&orderby=<?php if($orderby=='ASC'){print 'DESC';}else{print 'ASC';}?>&sortby=name">Name</a></td>
<?php if($_GET['page'] == "search"){?>
			<td><a class="title" href="index.php?page=<?php print $_GET['page']; ?>&file=<?php print $_GET['file']; ?>&server=<?php print $_GET['server']; ?>&orderby=<?php if($orderby=='ASC'){print 'DESC';}else{print 'ASC';}?>&sortby=pertinence">Pertinence</a></td>
<?php } ?>			
			<td><a class="title" href="index.php?page=<?php print $_GET['page']; ?>&file=<?php print $_GET['file']; ?>&server=<?php print $_GET['server']; ?>&orderby=<?php if($orderby=='ASC'){print 'DESC';}else{print 'ASC';}?>&sortby=taille">Size</a></td>
			<td><a class="title" href="index.php?page=<?php print $_GET['page']; ?>&file=<?php print $_GET['file']; ?>&server=<?php print $_GET['server']; ?>&orderby=<?php if($orderby=='ASC'){print 'DESC';}else{print 'ASC';}?>&sortby=date">Date</a></td>
			<td><a class="title" href="index.php?page=<?php print $_GET['page']; ?>&file=<?php print $_GET['file']; ?>&server=<?php print $_GET['server']; ?>&orderby=<?php if($orderby=='ASC'){print 'DESC';}else{print 'ASC';}?>&sortby=server">Server</a></td>
		</tr>
		<?php 
		while ($query = mysql_fetch_array($sql_query) ){

				?>
				<tr bgcolor="#ffffff" onmouseout="this.bgColor='#FFFFFF';" onmouseover="this.bgColor='#D0E8F8';" >	
				<td><a href="index.php?film=<?php print md5($query['name']); ?>&idserver=<?php print $query['server']; ?>&page=<?php print $_GET['page']; ?>&server=<?php print $_GET['server']; ?>"><?php print htmlspecialchars(stripslashes($query['name']));?></a></td>
<?php if($_GET['page'] == "search"){?>
				<td><?php print $query['found_count'];?></td>		
<?php } ?>				
				<td><?php print (round($query['taille']/1048576,2)); ?> Mo</td>
				<td><?php print date("d M y",$query['date']);?></td>
				<td><?php print $query['server'];?></td>
				</tr>
				<?php

		}//fin du while
		?>
		</table>
<?php		
	}
?>
</div>
<?php } ?>